package com.springboot_shopping.controller;

import com.springboot_shopping.entity.CartItem;
import com.springboot_shopping.entity.User;
import com.springboot_shopping.service.CartService;
import com.springboot_shopping.vo.CartItemVo;
import com.springboot_shopping.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartItemController {

    @Autowired
    private CartService cartService;

    // 添加商品到购物车
//    @PostMapping("/add")
//    public ResultVo addToCart(@RequestBody CartItem cartItem) {
//        try {
//            cartService.addToCart(cartItem);
//            return ResultVo.success("商品已成功添加到购物车", cartItem);
//        } catch (Exception e) {
//            return ResultVo.error("添加失败: " + e.getMessage());
//        }
//    }

    //todo:从Session获取用户信息，不再依赖前端传递的userId
    @PostMapping("/add")
    public ResultVo addToCart(@RequestBody CartItem cartItem, HttpSession session, HttpServletRequest request) {
        // 从session获取用户信息
        // 从session获取用户信息
        // 修改点2：统一使用"loginUser"键名获取用户对象
        User loginUser = (User) session.getAttribute("user");
        String userId = request.getHeader("userId");
        System.out.println(userId);
        if (userId == null) {
            return ResultVo.error("用户未登录");
        }

        // 设置用户ID
        cartItem.setUserId(Integer.valueOf(userId));

        try {
            cartService.addToCart(cartItem);
            return ResultVo.success("商品已成功添加到购物车", cartItem);
        } catch (Exception e) {
            return ResultVo.error("添加失败: " + e.getMessage());
        }
    }





    // 查询购物车列表
    @GetMapping("/list")
    public ResultVo getCartList(@RequestParam Integer userId
    ) {
        try {
            List<CartItemVo> cartItems = cartService.getCartItems(userId);
            return ResultVo.success("购物车查询成功", cartItems);
        } catch (Exception e) {
            return ResultVo.error("查询失败: " + e.getMessage());
        }
    }

    // 更新商品数量
    @PostMapping("/updateQuantity")
    public ResultVo updateQuantity(@RequestParam Integer cartItemId,
                                   @RequestParam Integer quantity) {
        try {
            cartService.updateCartItemQuantity(cartItemId, quantity);
            return ResultVo.success("商品数量已更新", null);
        } catch (Exception e) {
            return ResultVo.error("更新失败: " + e.getMessage());
        }
    }

    // 更新商品选中状态
    @PostMapping("/updateSelected")
    public ResultVo updateSelected(@RequestParam Integer cartItemId,
                                   @RequestParam Boolean selected) {
        try {
            cartService.updateSelectedStatus(cartItemId, selected);
            return ResultVo.success("选中状态已更新", null);
        } catch (Exception e) {
            return ResultVo.error("更新失败: " + e.getMessage());
        }
    }

    // 删除购物车商品
    @DeleteMapping("/remove")
    public ResultVo removeCartItem(@RequestParam Integer cartItemId) {
        try {
            cartService.removeCartItem(cartItemId);
            return ResultVo.success("商品已从购物车移除", null);
        } catch (Exception e) {
            return ResultVo.error("删除失败: " + e.getMessage());
        }
    }

    // 清空购物车
    @DeleteMapping("/clear")
    public ResultVo clearCart(@RequestParam Integer userId) {
        try {
            cartService.clearCart(userId);
            return ResultVo.success("购物车已清空", null);
        } catch (Exception e) {
            return ResultVo.error("清空失败: " + e.getMessage());
        }
    }
}